package com.lovo.view.emp;

import com.lovo.bean.CutPageBean;
import com.lovo.bean.EmpBean;
import com.lovo.javafxComponent.*;
import com.lovo.service.IEmpService;
import com.lovo.service.impl.EmpServiceImpl;

import javafx.scene.layout.Pane;

/**
 * 员工面板
 * @author lovo
 */
public class EmpPane extends Pane {

    /**
     * 员工表格
     */
    private LovoTable table;
    /**
     * 姓名查询文本框
     */
    private LovoTxt nameTxt;
    /**
     * 文化程度查询文本框
     */
    private LovoTxt educationTxt;
    /**
     * 所在部门查询文本框
     */
    private LovoTxt deptTxt;
    /**
     * 分页对象
     */
    private LovoCutPage cutObj;
    
    private IEmpService empService = new EmpServiceImpl();

    /**
     * 构造方法
     */
    public EmpPane() {
        this.initComponent();
    }

    /**
     * 初始化组件
     */
    public void initComponent() {
        new LovoTitle("员工管理", 0, 30, this);

        cutObj = new LovoCutPage(50, 350, this) {
            @Override
            public void clickEvent(int pageNO) {
                findEmpInfo(pageNO);
            }
        };

        LovoButton delButton = new LovoButton("删除员工", 80, 450, this);
        LovoButton updateButton = new LovoButton("修改员工信息", 220, 450, this);
        LovoButton addButton = new LovoButton("添加新员工", 80, 500, this);
        LovoButton showInfoButton = new LovoButton("查看员工信息", 220, 500, this);

        addButton.setOnAction(e -> showAddDialog());
        updateButton.setOnAction(e -> showUpdateDialog());
        showInfoButton.setOnAction(e -> showInfoDialog());
        delButton.setOnAction(e -> deleteEmp());
        LovoTitlePane tp = new LovoTitlePane("查询员工信息", 350, 400, 400, 170, this);
        this.nameTxt = new LovoTxt("员工姓名", 50, 30, tp);
        this.educationTxt = new LovoTxt("文化程度", 50, 80, tp);


        LovoButton findButton = new LovoButton("查询", 150, 130, tp);
        findButton.setOnAction(e -> findEmpInfo(1));
        initTable();
    }

    /**
     * 显示添加对话框
     */
    private void showAddDialog() {
        new AddEmpDialog(this);
    }
    /**
     * 显示修改对话框
     */
    private void showUpdateDialog(){
    	// 用户必须选中对应的员工才能进行修改
    	if(table.getSelectItem() == null) {
    		LovoDialog.showMessageDialog("请选择要修改的员工");
    		return;
    	}
        new UpdateEmpDialog(this,(EmpBean)table.getSelectItem());
    }
    /**
     * 显示员工信息对话框
     */
    private void showInfoDialog(){
    	// 用户必须选中对应的员工才能进行修改
    	if(table.getSelectItem() == null) {
    		LovoDialog.showMessageDialog("请选择要查看的员工");
    		return;
    	}
        new ShowEmpDialog(this,(EmpBean)table.getSelectItem());
    }

    private void deleteEmp(){
    	// 用户必须选中对应的员工才能进行删除
    	if(table.getSelectItem() == null) {
    		LovoDialog.showMessageDialog("请选择要删除的员工");
    		return;
    	}
    	// 弹出确认框
    	if(LovoDialog.showConfirmDialog("是否确认删除？") == 0) {
    		// 获取用户选中的员工对象
        	EmpBean empBean = (EmpBean)table.getSelectItem();
        	empService.delete(empBean.getId());
        	// 刷新表格
        	findEmpInfo(1);
    	}
    	
    	
    }

    //------------------------

    /**
     * 初始化表格
     */
    private void initTable() {
        this.table = new LovoTable<>(new String[]{
                "姓名:name", "性别:gender", "出生日期:born", "文化程度:education", "联系电话:phone","所在部门:deptObj.name"
        }, 10, 80, 780, 260, this);
        this.findEmpInfo(1);
    }

    /**
          * 查询员工信息
     */
    public void findEmpInfo(int pageNo) {
    	// 将员工信息获取到
    	CutPageBean cutPageBean = empService.findByItem(pageNo, nameTxt.getText(), educationTxt.getText());
    	// 更新表格数据
    	table.updateData(cutPageBean.getList());
    	if(pageNo == 1) {
        	// 更新分页信息
        	cutObj.setTotal(cutPageBean.getTotalPage());
    	}
    	
    }

    /**
     * 显示学生信息对话框
     */
    private void showEmpInfoDialog() {

    }

}
